<script>
import { Loading } from '@cci/cui'
import SpecialTree from '@/components/DatagroupTree'
import request from '@/utils/request.js'

let loadingInstance = {}

export default {
  name: 'roleAuthorize',
  inject: ['role', 'Dialog'],
  components: {
    SpecialTree
  },
  props: ['projectId'],
  methods: {
    getInitInfo() {
      this.$nextTick(() => {
        this.$refs.tree.load()
      })
    },
    getHalfANdAllCheckedList() {
      let allChecked = []
      allChecked.push(this.$refs.tree.getChecked())
      if (this.$refs.tree.getHalfChecked().length !== 0) {
        allChecked.push(this.$refs.tree.getHalfChecked())
      }
      return allChecked
    },
    getNextNode(params, url = this.getNextNodeUrl) {
      params.roleId = this.role.roleId
      return request({
        url,
        method: 'get',
        params
      })
    }
  },
  watch: {
    'Dialog.visible': {
      immediate: true,
      handler(val) {
        val && this.getInitInfo()
      }
    }
  },
  data() {
    return {
      emptyConfirm: 0,
      tree: {
        autoLoad: false,
        checkList: [],
        lazy: true,
        width: '400',
        height: '200',
        expand: false,
        accordion: false,
        isShowHeader: false,
        treeMenuButtonList: [],
        treeOperateButtonList: [],
        search: {},
        showCheckbox: true,
        selectTree: {},
        nextNodeKey: 'node',
        nextNodeValue: '',
        idKey: 'deptId',
        title: '',
        getNextChildNode: this.getNextNode
      },
      currentLabelKey: '当前角色',
      currentLabelValue: '',
      roleId: '',
      idKey: 'roleId',
      // getNextNodeUrl: '/v1/sys/role/findAllFunNodeTree',
      getNodeInfoUrl: '/v1/sys/role/findById'
    }
  },
  computed: {
    getNextNodeUrl() {
      var sessionObj = sessionStorage.getItem('userInfo_tc_admin_web')
      var userInfo = JSON.parse(sessionObj)
      if (userInfo.project) {
        var projectId = userInfo.project.projectId
        if (projectId) {
          this.projectId = projectId
        }
      }
      return this.projectId
        ? '/v1/sys/role/findAllProjectFunNodeTree'
        : '/v1/sys/role/findAllFunNodeTree'
    }
  },
  render(h) {
    return (
      <div class='roleAuthorize'>
        <div class='top'>
          {this.currentLabelKey}: {this.currentLabelValue}
        </div>
        <SpecialTree class='bottom' ref={'tree'} tree={this.tree}></SpecialTree>
      </div>
    )
  }
}
</script>

<style lang="scss">
.roleAuthorize {
  display: flex;
  flex-direction: column;
  height: 100%;
  .top {
    margin-bottom: 4px;
  }
  .bottom {
    flex: 1;
    margin: 0 100px;
    .el-scrollbar {
      max-height: 440px !important;
      min-height: 36px;
      height: 100% !important;
      .el-select-dropdown__wrap {
        margin-bottom: 0 !important;
        .el-select-dropdown__list {
          padding-top: 0;
        }
        .el-tree {
          margin-top: 0;
        }
      }
    }
  }
}
</style>
